module.exports = {
    prefix: "/wx/core/",
    post: {
        "prpayid": {
            des: {
                name: "getWxAppPayPrePayId",
                content: "获取微信App支付prepay_id",
                params: [
                    {
                        key: "appid",
                        type: "string",
                    },
                    {
                        key: "mch_id",
                        type: "string|Number",
                    },
                    {
                        key: "nonce_str", // 随机字符串，放重发，现在由服务端生成
                        type: "string",
                        length: "32"
                    },
                    {
                        key: "body", // 商品描述
                        type: "string",
                        length: "128"
                    },
                    {
                        key: "out_trade_no", // 订单号
                        type: "string",
                        length: "32"
                    },
                    {
                        key: "total_fee", // 订单总金额，单位是分不是元
                        type: "Number",
                    },
                    {
                        key: "spbill_create_ip", // 客户端ip,现在这个参数由服务端获取，客户端不必发送
                        type: "string",
                    },
                    {
                        key: "notify_url", // 回调地址 
                        type: "string",
                    },
                    {
                        key: "trade_type", // 支付方式，App端请发送'APP'
                        type: "string",
                    },
                    {
                        key: "key", // API密钥
                        type: "string",
                    },
                    {
                        key: "attach", // bus_id微站分配的商户id
                        type: "string",
                    }
                ]
            },
            method: function* () {
                var self = this;
                try {
                    var data = yield parse(self.request);
                    data.notify_url = "http://wx.dotnar.com/wx/core/pay_notify";
                    data.nonce_str = $.randomString(32);
                    delete data.bus_id; // 过滤掉IOS版本默认发送的商户id字段，微信服务器不能解析此字段

                    var key = data.key,
                        json = {};

                    if (!data.prepay_id) {
                        data.total_fee = Math.floor(data.total_fee);
                        data.spbill_create_ip = self.headers["x-forwarded-for"] || self.socket.remoteAddress;
                        data.sign = WX.getSign(data); // 获取第一次参数签名
                        json = $.xml2json(yield WX.getPrepayId(data)); // 获取prepayid，并将微信返回的xml数据转为json
                    };

                    json.package = "Sign=WXPay";
                    json.partnerid = data.mch_id;
                    json.noncestr = data.nonce_str;
                    json.timestamp = Math.floor(new Date().getTime() / 1000);
                    json.signValue = WX.getSign({  // 创建第二次签名所需参数包
                        appid: data.appid,
                        package: "Sign=WXPay",
                        partnerid: data.mch_id,
                        prepayid: data.prepay_id || json.prepay_id,
                        noncestr: data.nonce_str,
                        key: key,
                        timestamp: json.timestamp
                    });
                    self.body = json;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        },
        "pay_notify": {
            des: {
                name: "WXpayResultNotify",
                content: "微信支付结果通知回调，此接口为微信自动调用，不可人为调用"
            },
            xmlParser: true,
            method: function* () {
                var self = this;
                var xml = self.request.body; // 微信支付结果返回的数据包
                var json = $.xml2json(xml);  // 将返回的xml数据包转为json
                if (json.result_code === "SUCCESS") {
                    printSuccess("支付成功：", json);
                    var result = yield fetch("http://api.dotnar.com/wx/app_pay_notify", {
                        method: "POST",
                        headers: {
                            "Content-Type": "application/x-www-form-urlencoded"
                        },
                        body: jsonTOurlencoded(json)
                    });
                    result = yield result.json();
                    console.log("result:", result);
                };
                self.body = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>"
            }
        }
    },
    get: {
        "prpayid": {
            des: {
                name: "getWxAppPayPrePayId",
                content: "获取微信App支付prepay_id",
                params: []
            },
            method: function* () {
                var self = this;
                try {
                    var data = self.request.query;
                    data.notify_url = data.notify_url || "http://wx.dotnar.com/wx/core/pay_notify";
                    data.nonce_str = $.randomString(32);
                    delete data.bus_id; // 过滤掉IOS版本默认发送的商户id字段，微信服务器不能解析此字段

                    var key = data.key,
                        json = {};

                    if (!data.prepay_id) {
                        data.total_fee = Math.floor(data.total_fee);
                        data.spbill_create_ip = self.headers["x-forwarded-for"] || self.socket.remoteAddress;
                        data.sign = WX.getSign(data); // 获取第一次参数签名
                        json = $.xml2json(yield WX.getPrepayId(data)); // 获取prepayid，并将微信返回的xml数据转为json
                    };

                    json.package = "Sign=WXPay";
                    json.partnerid = data.mch_id;
                    json.noncestr = data.nonce_str;
                    json.timestamp = Math.floor(new Date().getTime() / 1000);
                    json.signValue = WX.getSign({  // 创建第二次签名所需参数包
                        appid: data.appid,
                        package: "Sign=WXPay",
                        partnerid: data.mch_id,
                        prepayid: data.prepay_id || json.prepay_id,
                        noncestr: data.nonce_str,
                        key: key,
                        timestamp: json.timestamp
                    });
                    self.body = json;
                } catch (e) {
                    self.body = printError(e);
                };
            }
        },
    }
}